package com.his.struts.action;

import java.sql.ResultSet;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.his.struts.dao.friendlink.FriendLinkDao;
import com.his.struts.dao.friendlink.FriendLinkImp;
import com.his.struts.form.AnswerForm;
import com.his.struts.form.FriendlinkForm;
import com.his.struts.util.PageUtil;

/**
 * 友情链接功能类
 * @author liumiaoyue
 *
 */
public class FriendlinkAction extends DispatchAction {
	FriendLinkDao dao = new FriendLinkImp();

	/**
	 * Method getAllLink
	 * 得到所有链接
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward getAllLink(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		//判断链接是否为空
		String id=request.getParameter("fid");
		if(id==null){
			id="";
		}else{
			int fid =Integer.parseInt(id);
			//删除链接
			dao.delLink(fid);
			request.setAttribute("message", "删除成功");
		}
		
		String sql = "friendlink order by fid desc";
		// 调用分页类 
		ResultSet rs = PageUtil.getPage(sql, 10, request);
		if(rs != null){
			// 封装
			List<FriendlinkForm> list = dao.getLinkBean(rs);
			// 数据放到request中
			request.setAttribute("linkList",list);
		}
		
		
		return mapping.findForward("allLink");
	}
	/**
	 * Method addLink
	 * 添加链接
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward addLink(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		//FriendlinkForm 实体类
		FriendlinkForm linkForm=(FriendlinkForm)form;
		//添加链接
		dao.addLink(linkForm);
		
		String sql = "friendlink order by fid desc";
		// 调用分页类 
		ResultSet rs = PageUtil.getPage(sql, 10, request);
		if(rs != null){
			// 封装
			List<FriendlinkForm> list = dao.getLinkBean(rs);
			// 数据放到request中
			request.setAttribute("linkList",list);
		}
		request.setAttribute("message", "添加成功");
		return mapping.findForward("allLink");
	}
	/**
	 * Method addLink
	 * 更新链接
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward updateLink(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		//FriendlinkForm 实体类
		FriendlinkForm linkForm=(FriendlinkForm)form;
		//更新链接
		dao.updateLink(linkForm);
		
		String sql = "friendlink order by fid desc";
		// 调用分页类 
		ResultSet rs = PageUtil.getPage(sql, 10, request);
		if(rs != null){
			// 封装
			List<FriendlinkForm> list = dao.getLinkBean(rs);
			// 数据放到request中
			request.setAttribute("linkList",list);
		}
		request.setAttribute("message", "修改成功");
		return mapping.findForward("allLink");
	}
	
	
}